<template>
  <div>
    <div ref="echarts" style="width: 600px; height: 400px;"></div>
  </div>
</template>

<script>
import * as echarts from 'echarts'
export default {
  name: 'EChartsComponent',
  data () {
    return {
      myChart: undefined
    }
  },
  mounted () {
    this.myChart = echarts.init(this.$refs.echarts)
    const data = [5, 20, 36, 10, 10, 20];
    this.myChart.setOption({
      title: {
        text: '动态排序柱状图'
      },
      tooltip: {},
      xAxis: {
        // 数据的最大值作为 X 轴最大值
        max: 'dataMax'
      },
      yAxis: {
        // 表示 Y 轴从下往上是从小到大的排列
        inverse: true,
        type: 'category',
        data: ['A', 'B', 'C', 'D', 'E', 'F'],
        // 想只显示前 n 名，将 yAxis.max 设为 n - 1，否则显示所有柱条
        // max: 2
      },
      series: [
        {
          // 实时排序
          realtimeSort: true,
          name: 'X',
          type: 'bar',
          data: data,
        }
      ],
    })
    const run = () => {
      for (let i = 0; i < data.length; ++i) {
        if (Math.random() > 0.9) {
          data[i] += Math.round(Math.random() * 2000);
        } else {
          data[i] += Math.round(Math.random() * 200);
        }
      }
      this.myChart.setOption({
        series: [
          {
            type: 'bar',
            data
          }
        ]
      });
    }
    setInterval(() => {
      run();
    }, 3000);
  },
  methods: {
  }
}
</script>
<style lang="scss" scoped></style>
